package cn.nc.service;


import cn.nc.entity.Article;
import cn.nc.entity.User;
import cn.nc.repository.ArticleRepository;
import cn.nc.repository.CommentRepository;
import cn.nc.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    CommentRepository commentRepository;

    //分页展示  前台展示文章
    public Page<Article> pageArticle(int page,int size){
        Pageable pageable = PageRequest.of(page,size);


        return articleRepository.findAll(pageable);
    }

    //根据用户分页展示



    //根据ID查询该文章   详情显示
    public Article findArticleById(Integer id){
        if(articleRepository.findById(id).isPresent()){
            return articleRepository.findById(id).get();
        }else return null;
    }


    //删除文章
    public void deleteByArticleId(Integer id){
        Article article = findArticleById(id);

        commentRepository.deleteByArticle(article);
        articleRepository.deleteArticleById(article.getId());
    }


    //查询所有文章
    public List<Article> findAll(){
        return articleRepository.findAll();
    }

    //修改文章
    public void updateArticleById(Integer id,String title,String content){
        Article article = findArticleById(id);
        article.setTitle(title);
        article.setContent(content);
        articleRepository.save(article);
    }


    //新增文章
    public Article addArticle(@RequestParam(name="title") String title, @RequestParam(name="content") String content){
        Article article = new Article();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = userRepository.findById(((User)authentication.getPrincipal()).getId()).get();

        article.setTitle(title);
        article.setContent(content);
        article.setDate(new Date());
        article.setUser(user);
        return articleRepository.save(article);
    }



    //条件分页
//    public List<Article> getPageData(Integer pageNum,Integer pageSize,Integer userId){
//
//        Specification<Article> spec = new Specification<Article>() {
//            @Override
//            public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
//                return query.where(builder.equal(root.get("user").get("id"),userId)).getRestriction();
//            }
//        };
//
//
//        List<Article> articles = null;
//
//        PageRequest pageRequest = PageRequest.of(pageNum-1,pageSize);
//        Page<Article> page = null;
//
//        page = articleRepository.findAll(spec,pageRequest);
//
//
//        articles = page.getContent();
//        int total = page.getTotalPages();
//
//        System.out.println(articles);
//        System.out.println(total);
//
//        return articles;
//
//
//    }

}
